package com.sunwayworld.basemodule.business.outsource.io;

import com.sunwayworld.basemodule.basedata.exporttemplate.service.LimsGenericTemplateService;
import com.sunwayworld.basemodule.business.outsource.bean.LimsOutsourceSampleBean;
import com.sunwayworld.basemodule.business.outsource.service.LimsOutsourceSampleService;
import com.sunwayworld.framework.io.excel.utils.ExcelUtils;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.BeanUtils;
import com.sunwayworld.framework.utils.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;

/**
 * @ClassName LimsOutsourceResultExportTemplate
 * @Author zhoujq
 * @Date 2023/3/16
 * @Version 1.0
 **/
@Component(value = "LimsOutsourceResultExportTemplate")
@GikamBean
public class LimsOutsourceResultExportTemplate implements LimsGenericTemplateService<String> {

    @Autowired
    @Lazy
    private LimsOutsourceSampleService limsOutsourceSampleService;

    @Override
    public void initTemplateData(Path path, String id) {
        if (StringUtils.isEmpty(id)) {
            return;
        }
        List<LimsOutsourceSampleBean> results = limsOutsourceSampleService.selectListByFilter(SearchFilter.instance().match("applyId", id).filter(MatchPattern.EQ));
        try {
            InputStream in = new FileInputStream(path.toString());
            Workbook workBook = WorkbookFactory.create(in);
            Sheet sheet = workBook.getSheetAt(0);

            if (!results.isEmpty()) {
                addInfo(sheet, workBook, results);
            }

            in.close();
            FileOutputStream outputStream = new FileOutputStream(path.toString());
            workBook.write(outputStream);
            outputStream.close();
            workBook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void addInfo(Sheet sheet, Workbook wb, List<LimsOutsourceSampleBean> results) {
        Row titleRow = sheet.getRow(0);
        int colNum = titleRow.getPhysicalNumberOfCells();
        int firstRow = 1;

        CellStyle centerCellStyle = ExcelUtils.getCenterCellStyle(wb);
        centerCellStyle.setBorderTop(BorderStyle.THIN);
        centerCellStyle.setBorderBottom(BorderStyle.THIN);

        for (int i = 0; i < results.size(); i++) {
            LimsOutsourceSampleBean bean = results.get(i);
            Row row = sheet.createRow(i + firstRow);
            for (int j = 1; j < colNum; j++) {
                try {
                    String column = titleRow.getCell(j).getCellComment().getString().getString();
                    Object v = BeanUtils.getPropertyValue(bean, column);
                    if (v != null) {
                        Cell cell = row.createCell(j);
                        cell.setCellStyle(centerCellStyle);
                        cell.setCellValue(v.toString());
                    }
                } catch (Exception e) {

                }
            }
        }
    }
}
